//
//  LDYToolBarView.swift
//  李东阳微博
//
//  Created by 李东阳 on 16/4/21.
//  Copyright © 2016年 李东阳. All rights reserved.
//

import UIKit

class LDYToolBarView: UIView {


      var retweetButton : UIButton?
    
      var commentButton : UIButton?
    
      var unlikeButton : UIButton?
    
    
    var status:LDYStatus?{
  
    
        didSet{
        
            let retweetResult = getCountString(status?.reposts_count ?? 0, title: "转发")
            
            let commentResult =  getCountString(status?.comments_count ?? 0, title: "评论")
            
           let unlikeResult =  getCountString(status?.attitudes_count ?? 0, title: "赞")
        
            retweetButton?.setTitle(retweetResult, forState: UIControlState.Normal)
             commentButton?.setTitle(commentResult, forState: UIControlState.Normal)
             unlikeButton?.setTitle(unlikeResult, forState: UIControlState.Normal)
        }
    
    }
    
    
    
    
    override  init(frame: CGRect) {
        super.init(frame: frame)

        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
    func getCountString(count:Int, title:String) -> String{
        if count <= 0{
        
        return title
            
        }else if count > 0 && count < 10000{
        
        
        return "\(count)"
        }else{
        
        let num = CGFloat( count/1000 )/10
        
            var countString = "\(num)万"
            
            if countString.containsString(".0"){
            
            countString = countString.stringByReplacingOccurrencesOfString(".0万", withString: "万")
            }
            return countString
        }
        
    
    }
    
    
    
    
    
        //MARK: -  设置视图
    private  func  setupUI(){
    
     self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
//        self.backgroundColor = UIColor.redColor()
        // 01-添加控件
         retweetButton = addSubutton("timeline_icon_retweet", title: "转发")
         commentButton =  addSubutton("timeline_icon_comment", title: "评论")
         unlikeButton =  addSubutton("timeline_icon_unlike", title: "赞")
        
        
            //MARK: -  添加两根竖线
        let line1 = addSubImageView()
        let line2 = addSubImageView()
        
        
            //MARK: -  转发按钮 =============
        retweetButton!.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
            
        }
        
            //MARK: -  评论按钮 =============
        commentButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.width.equalTo(unlikeButton!)
            
        }
        
        
            //MARK: -  赞按钮 =============
        unlikeButton!.snp_makeConstraints { (make) -> Void in
            make.trailing.top.bottom.equalTo(self)
           make.leading.equalTo(commentButton!.snp_trailing)
            
        }
        
             //MARK: -  line1 =============
        
        line1.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(commentButton!.snp_leading)
            make.centerY.equalTo(commentButton!)
            
        }
        
        
             //MARK: - line2 =============
        line2.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(commentButton!.snp_trailing)
            make.centerY.equalTo(commentButton!)
            
        }

    }
    
    
    

        //MARK: -  创建button
    private func addSubutton(imaName:String, title:String) -> UIButton{
 "TODO:|FIXME: 自定义一个方法 添加相同类型的button"
            //MARK: -  初始化button
        let button = UIButton()
        
            //MARK: -  设置文字
     button.setTitle(title, forState: UIControlState.Normal)
     button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
      button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        // 设置image
        button.setImage(UIImage(named: imaName), forState: UIControlState.Normal)
            //MARK: -  设置背景
      button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
     
      button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        
        addSubview(button)
        
       return button
        
    }

    private func addSubImageView() -> UIImageView{
    
        let img = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(img)
        
        return img
    
    }
    
    
    
    
    
    
    
    
    
}
